﻿using OpenMms.Common.Log;
using OpenMms.Domain.Interface;
using OpenMms.Domain.Interface.User;
using OpenMms.Domain.Models;
using OpenMms.Domain.ReponseModels;
using OpenMms.Domain.RequestModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;

namespace OpenMms.Domain.BLL.User
{
    /// <summary>
    /// 会员升级业务逻辑
    /// </summary>
    public class UserUpgradeService: IUserUpgradeService
    {
        #region 注入
        private IBLLConstruct<UserInforMations> _userInfo;
        private IBLLConstruct<MemberLevel> _memberLevel;
        private IBLLConstruct<Models.UserUpgrade> _UserUpgrade;

        public UserUpgradeService(IBLLConstruct<UserInforMations> userInfo
           , IBLLConstruct<MemberLevel> memberLevel
            , IBLLConstruct<UserUpgrade> UserUpgrade
            )
        {
            this._UserUpgrade = UserUpgrade;
            this._userInfo = userInfo;
            this._memberLevel = memberLevel;
        }
        #endregion

        #region 会员升级操作
        /// <summary>
        /// 会员升级操作
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Response> Do(UserUpGradeReq model)
        {
            var resp = new Response();
            resp.Code = 200;
            resp.Message = "升级成功";
            if(! await IsRepeat(model))
            {
                resp.Code = 201;
                resp.Message = "重复提交";
                return resp;
            }
            MyLog.Log(model, "User", "会员升级");
            await UpApply(model);
            return resp;

        }
        #endregion

        #region 获取会员信息
        /// <summary>
        /// 获取会员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<UserInforMations> GetInforMations(string code)
        {
            return await _userInfo.FindAsync(s=>s.UserCode==code);
        }
        #endregion

        #region 可晋升级别
        /// <summary>
        /// 获取可以晋升的级别
        /// </summary>
        /// <returns></returns>
        public async Task<IList<MemberLevelViewModel>> QueryMemberLevel(int level)
        {
            var result = await _memberLevel.DesignQueryAsync<MemberLevelViewModel>(s => s.LevelId > level, s => new MemberLevelViewModel()
            {
                Amount = s.Amount ?? 0,
                LevelID = s.LevelId ?? 0,
                LevelName = s.LevelName,
                SelectFileds = s.LevelName + "|" + (double)s.Amount,
                TotalAchie = s.TotalAchie ?? 0,
                TotalNumber = s.TotalNumber ?? 0

            });
            return result;
        }
        #endregion

        #region 判断是否重复提交
        /// <summary>
        /// 判断是否重复提交
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private async Task<bool> IsRepeat(UserUpGradeReq model)
        {
            var result = await _UserUpgrade.CountAsync(s => s.UpgradeState == 0 && s.UserCode == model.UserCode);
            return result == 0;
        }
        #endregion

        #region 计算差额
        /// <summary>
        /// 计算差额
        /// </summary>
        /// <param name="model"></param>
        public async Task<decimal> CalcAmount(UserUpGradeReq model)
        {
            var afterAmount = await _memberLevel.DesignQueryAsync(s => s.LevelId == model.AfterLevel,s=>s.Amount??0);
            var nowAmount = await _memberLevel.DesignQueryAsync(s => s.LevelId == model.NowLevel,s=>s.Amount??0);
            return afterAmount.FirstOrDefault() - nowAmount.FirstOrDefault();
        }
        #endregion

        #region 写入会员升级表
        /// <summary>
        /// 写入会员升级表
        /// </summary>
        /// <param name="model"></param>
        /// <param name="hsTable"></param>
        private async Task UpApply(UserUpGradeReq req)
        {
          await  _UserUpgrade.InsertAsync(new Models.UserUpgrade()
            {
                AfterLevel = (byte)req.AfterLevel,
                Amount = req.Amount,
                NowLevel = (byte)req.NowLevel,
                UpgradeDate = req.UpgradeDate,
                UpgradeState = (byte)req.UpgradeState,
                UserCode = req.UserCode
            });
        }
        #endregion

        #region 查询升级记录
        /// <summary>
        /// 查询升级记录
        /// </summary>
        /// <returns></returns>
        public async Task<IPagedList<UserUpgrade>> UpRecord(string code, int? pageIndex)
        {
            return await _UserUpgrade.PageQueryAsync(s => s.UserCode == code, pageIndex,s=>s.Id,true);
        }
        #endregion

    }
}